package cn.pconline.activity.util;



import java.math.BigInteger;
import java.util.UUID;

public class Password {

    private static TEA createTEA() {
        byte abyte0[] = (new BigInteger(PASSWORD_KEY, 16)).toByteArray();
        return new TEA(abyte0);
    }

    public static String encodePassword(String s) {
        TEA tea = createTEA();
        return tea.encode(s);
    }

    public static String decodePassword(String s) {
        TEA tea = createTEA();
        return tea.decode(s);
    }

    private static final String PASSWORD_KEY = "391858f86df9b989a8c87cb8d9ad599";

    public static void main(String[] args) {
        System.out.println(encodePassword(UUID.randomUUID().toString()));
        //System.out.println(decodePassword("CA64A39A7E2D970298B8A815020BA042E95FCE9A7424EDD9041948A589BDBF4CBDF93693EC8EA94AAB02D6B59321C7B1"));
        //System.out.println(decodePassword("EB22B6E5F67535E4"));
    }

}

class TEA {

    public TEA(byte abyte0[]) {
        int i = abyte0.length;
        _key = new int[4];
        if (i != 16)
            throw new ArrayIndexOutOfBoundsException(getClass().getName() + ": Key is not 16 bytes");
        int k = 0;
        for (int j = 0; j < i;) {
            _key[k] = abyte0[j] << 24 | (abyte0[j + 1] & 0xff) << 16 | (abyte0[j + 2] & 0xff) << 8 | abyte0[j + 3] & 0xff;
            j += 4;
            k++;
        }

        _keyBytes = abyte0;
    }

    public TEA(int ai[]) {
        _key = ai;
    }

    public String toString() {
        String s = getClass().getName();
        s = s + ": Tiny Encryption Algorithm (TEA)  key: " + getHex(_keyBytes);
        return s;
    }

    public int[] encipher(int ai[]) {
        int i = ai[0];
        int j = ai[1];
        int k = 0;
        int l = 0x9e3779b9;
        for (int i1 = 32; i1-- > 0;) {
            i += (j << 4 ^ j >>> 5) + (j ^ k) + _key[k & 3];
            k += l;
            j += (i << 4 ^ i >>> 5) + (i ^ k) + _key[k >>> 11 & 3];
        }

        int ai1[] = new int[2];
        ai1[0] = i;
        ai1[1] = j;
        return ai1;
    }

    public int[] decipher(int ai[]) {
        int i = ai[0];
        int j = ai[1];
        int k = 0xc6ef3720;
        int l = 0x9e3779b9;
        for (int i1 = 32; i1-- > 0;) {
            j -= (i << 4 ^ i >>> 5) + (i ^ k) + _key[k >>> 11 & 3];
            k -= l;
            i -= (j << 4 ^ j >>> 5) + (j ^ k) + _key[k & 3];
        }

        int ai1[] = new int[2];
        ai1[0] = i;
        ai1[1] = j;
        return ai1;
    }

    public String encode(String s) {
        byte abyte0[] = s.getBytes();
        int ai[] = encode(abyte0, abyte0.length);
        return binToHex(ai);
    }

    public String decode(String s) {
        byte abyte0[] = decode(hexToBin(s));
        return new String(abyte0);
    }

    public int[] encode(byte abyte0[], int i) {
        int l = i;
        byte abyte1[] = abyte0;
        _padding = l % 8;
        if (_padding != 0) {
            _padding = 8 - l % 8;
            abyte1 = new byte[l + _padding];
            System.arraycopy(abyte0, 0, abyte1, 0, l);
            l = abyte1.length;
        }
        int i1 = l / 4;
        int ai[] = new int[2];
        int ai1[] = new int[i1];
        int k = 0;
        for (int j = 0; j < l;) {
            ai[0] = abyte1[j] << 24 |
                    (abyte1[j + 1] & 0xff) << 16 |
                    (abyte1[j + 2] & 0xff) << 8 |
                    abyte1[j + 3] & 0xff;
            ai[1] = abyte1[j + 4] << 24 |
                    (abyte1[j + 5] & 0xff) << 16 |
                    (abyte1[j + 6] & 0xff) << 8 |
                    abyte1[j + 7] & 0xff;
            ai = encipher(ai);
            ai1[k] = ai[0];
            ai1[k + 1] = ai[1];
            j += 8;
            k += 2;
        }

        return ai1;
    }

    public int padding() {
        return _padding;
    }

    public byte[] decode(byte abyte0[], int i) {
        int l = i / 4;
        int ai[] = new int[l];
        int j = 0;
        for (int k = 0; j < l; k += 8) {
            ai[j] = abyte0[k] << 24 | (abyte0[k + 1] & 0xff) << 16 | (abyte0[k + 2] & 0xff) << 8 | abyte0[k + 3] & 0xff;
            ai[j + 1] = abyte0[k + 4] << 24 | (abyte0[k + 5] & 0xff) << 16 | (abyte0[k + 6] & 0xff) << 8 | abyte0[k + 7] & 0xff;
            j += 2;
        }

        return decode(ai);
    }

    public byte[] decode(int ai[]) {
        int i = ai.length;
        byte abyte0[] = new byte[i * 4];
        int ai1[] = new int[2];
        int k = 0;
        for (int j = 0; j < i;) {
            ai1[0] = ai[j];
            ai1[1] = ai[j + 1];
            ai1 = decipher(ai1);
            abyte0[k] = (byte) (ai1[0] >>> 24);
            abyte0[k + 1] = (byte) (ai1[0] >>> 16);
            abyte0[k + 2] = (byte) (ai1[0] >>> 8);
            abyte0[k + 3] = (byte) ai1[0];
            abyte0[k + 4] = (byte) (ai1[1] >>> 24);
            abyte0[k + 5] = (byte) (ai1[1] >>> 16);
            abyte0[k + 6] = (byte) (ai1[1] >>> 8);
            abyte0[k + 7] = (byte) ai1[1];
            j += 2;
            k += 8;
        }

        return abyte0;
    }

    public int[] hexToBin(String s)
            throws ArrayIndexOutOfBoundsException {
        int i = s.length();
        if (i % 8 != 0)
            throw new ArrayIndexOutOfBoundsException("Hex string has incorrect length, required to be divisible by eight: " + i);
        int j = i / 8;
        int ai[] = new int[j];
        byte abyte0[] = new byte[2];
        byte abyte1[] = new byte[4];
        int k = 0;
        for (int l = 0; l < j; l++) {
            for (int i1 = 0; i1 < 4; i1++) {
                for (int j1 = 0; j1 < 2; j1++)
                    switch (s.charAt(k++)) {
                        case 48: // '0'
                            abyte0[j1] = 0;
                            break;

                        case 49: // '1'
                            abyte0[j1] = 1;
                            break;

                        case 50: // '2'
                            abyte0[j1] = 2;
                            break;

                        case 51: // '3'
                            abyte0[j1] = 3;
                            break;

                        case 52: // '4'
                            abyte0[j1] = 4;
                            break;

                        case 53: // '5'
                            abyte0[j1] = 5;
                            break;

                        case 54: // '6'
                            abyte0[j1] = 6;
                            break;

                        case 55: // '7'
                            abyte0[j1] = 7;
                            break;

                        case 56: // '8'
                            abyte0[j1] = 8;
                            break;

                        case 57: // '9'
                            abyte0[j1] = 9;
                            break;

                        case 65: // 'A'
                            abyte0[j1] = 10;
                            break;

                        case 66: // 'B'
                            abyte0[j1] = 11;
                            break;

                        case 67: // 'C'
                            abyte0[j1] = 12;
                            break;

                        case 68: // 'D'
                            abyte0[j1] = 13;
                            break;

                        case 69: // 'E'
                            abyte0[j1] = 14;
                            break;

                        case 70: // 'F'
                            abyte0[j1] = 15;
                            break;

                        case 97: // 'a'
                            abyte0[j1] = 10;
                            break;

                        case 98: // 'b'
                            abyte0[j1] = 11;
                            break;

                        case 99: // 'c'
                            abyte0[j1] = 12;
                            break;

                        case 100: // 'd'
                            abyte0[j1] = 13;
                            break;

                        case 101: // 'e'
                            abyte0[j1] = 14;
                            break;

                        case 102: // 'f'
                            abyte0[j1] = 15;
                            break;
                    }

                abyte1[i1] = (byte) (abyte0[0] << 4 | abyte0[1]);
            }

            ai[l] = abyte1[0] << 24 | (abyte1[1] & 0xff) << 16 | (abyte1[2] & 0xff) << 8 | abyte1[3] & 0xff;
        }

        return ai;
    }

    public String binToHex(int ai[])
            throws ArrayIndexOutOfBoundsException {
        if (ai.length % 2 == 1)
            throw new ArrayIndexOutOfBoundsException("Odd number of ints found: " + ai.length);
        StringBuffer stringbuffer = new StringBuffer();
        byte abyte0[] = new byte[8];
//        int ai1[] = new int[2];
//        int i = ai.length / 2;
        for (int j = 0; j < ai.length; j += 2) {
            abyte0[0] = (byte) (ai[j] >>> 24);
            abyte0[1] = (byte) (ai[j] >>> 16);
            abyte0[2] = (byte) (ai[j] >>> 8);
            abyte0[3] = (byte) ai[j];
            abyte0[4] = (byte) (ai[j + 1] >>> 24);
            abyte0[5] = (byte) (ai[j + 1] >>> 16);
            abyte0[6] = (byte) (ai[j + 1] >>> 8);
            abyte0[7] = (byte) ai[j + 1];
            stringbuffer.append(getHex(abyte0));
        }

        return stringbuffer.toString();
    }

    public String getHex(byte abyte0[]) {
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < abyte0.length; i++) {
            int j = abyte0[i] >>> 4 & 0xf;
            stringbuffer.append(hex[j]);
            j = abyte0[i] & 0xf;
            stringbuffer.append(hex[j]);
        }

        return stringbuffer.toString();
    }

    public String padPlaintext(String s, char c) {
        StringBuffer stringbuffer = new StringBuffer(s);
        int i = stringbuffer.length() % 8;
        for (int j = 0; j < i; j++)
            stringbuffer.append(c);

        return stringbuffer.toString();
    }

    public String padPlaintext(String s) {
        return padPlaintext(s, ' ');
    }

    private int _key[] = null;
    private byte _keyBytes[] = null;
    private int _padding = 0;
    protected static final char hex[] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F'
    };
}
